package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.AdminMapper;
import com.xxxx.server.mapper.AdminRoleMapper;
import com.xxxx.server.mapper.RoleMapper;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.AdminRole;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private AdminRoleMapper adminRoleMapper;

	/**
	 * 根据用户id查询角色
	 * @param adminId
	 * @return
	 */
	@Override
	public List<Role> getAllRolesByAdminId(Integer adminId) {
		return roleMapper.getAllRolesByAdminId(adminId);
	}

	/**
	 * 更新操作员角色
	 * @param adminId
	 * @param rids
	 * @return
	 */
	@Override
	@Transactional
	public RespBean updateAdminById(Integer adminId, Integer[] rids) {
		adminRoleMapper.delete(new QueryWrapper<AdminRole>().eq("adminId",adminId));
		Integer result = adminRoleMapper.addAdminRole(adminId,rids);
		//当影响的行数和修改的行数一样时，就更新成功
		if (rids.length == result) {
			return RespBean.success("更新成功");
		}
		return RespBean.error("更新失败");
	}
}
